package com.gome.spiderkit.servlet;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gome.spiderkit.data.SpiderkitInfoConnection;
import com.gome.spiderkit.model.GroupSummary;
import com.gome.spiderkit.task.monitor.MonitorInit;

public class GroupSummaryServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String zkhost = req.getParameter("zkhost");
		req.setAttribute("zkhost", zkhost);
		req.setAttribute("connect_failed", false);
		req.setAttribute("getinfo_failed", false);
		
		SpiderkitInfoConnection skinfoClient = new SpiderkitInfoConnection();
		
		GroupSummary gsummary;

		try {
			skinfoClient.connect(zkhost);
		} catch (Exception e) {
			e.printStackTrace();
			req.setAttribute("connect_failed", true);
			req.getRequestDispatcher("group.jsp").forward(req, resp);
			return;
		}

		String gname = req.getParameter("gname");
		if (gname != null) {
			skinfoClient.deleteNullGroup(gname);
		}
		
		try {
			skinfoClient.updateFromServer();
			gsummary = skinfoClient.getGroupSummary();
			req.setAttribute("gsummary", gsummary);
		} catch (Exception e) {
			e.printStackTrace();
			req.setAttribute("getinfo_failed", true);
			req.getRequestDispatcher("group.jsp").forward(req, resp);
			return;
		}
		
		req.getRequestDispatcher("group.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
